define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/team/index' + location.search,
                    add_url: 'user/team/add',
                    edit_url: 'user/team/edit',
                    del_url: 'user/team/del',
                    multi_url: 'user/team/multi',
                    import_url: 'user/team/import',
                    table: 'user_team',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'team_count', title: __('Team_count')},
                        {field: 'drive_count', title: __('Drive_count')},
                        {field: 'rate', title: __('Rate'), operate:'BETWEEN'},
                        {field: 'total', title: __('Total'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},
                        {field: 'created_time', title: __('Created_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'user.id', title: __('User.id')},
                        {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        {field: 'user.avatar', title: __('User.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table,events: $.extend(Table.api.events.operate, Controller.api.events.operate), buttons: [
                                {
                                    name: 'grant',
                                    text: __('发放'),
                                    title: __('团队收益发放'),
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-grant',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == 1) {
                                            return false
                                        }
                                        return true;
                                    }

                                    // url: 'user/team/grant',
                                    // callback: function (data) {
                                    //     Fast.api.close();
                                    // },
                                    // visible: function (row) {
                                    //     //返回true时按钮显示,返回false隐藏
                                    //     if(row.status == 1) {
                                    //         return false
                                    //     }
                                    //     return true;
                                    // }
                                }], formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {

            events: {
                operate: {
                    'click .btn-grant': function (e, value, row) {
                        e.stopPropagation();
                        e.preventDefault();
                        // 这里写ajax
                        var that = this;
                        var top = $(that).offset().top - $(window).scrollTop();
                        var left = $(that).offset().left - $(window).scrollLeft() - 260;
                        if (top + 154 > $(window).height()) {
                            top = top - 154;
                        }
                        if ($(window).width() < 480) {
                            top = left = undefined;
                        }
                        Layer.confirm('确认发放？',
                            {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                            function (index) {
                                Fast.api.ajax({url: 'user/team/grant?ids=' + row.id }, function () {
                                    $('#table').bootstrapTable('refresh');
                                });
                                Layer.close(index);
                            }
                        );
                    }
                },

                bindevent: function () {
                    Form.api.bindevent($("form[role=form]"));
                }


            }
        }
    };
    return Controller;
});